package com.smartmedical.doctor.interrogation.controller;

import com.smartmedical.common.core.constant.TokenConstants;
import com.smartmedical.common.core.domain.HealthR;
import com.smartmedical.common.security.service.TokenService;
import com.smartmedical.doctor.interrogation.pojo.entity.InquiryRecordEntity;
import com.smartmedical.doctor.interrogation.pojo.vo.*;
import com.smartmedical.doctor.interrogation.service.InterrogationService;
import com.smartmedical.system.api.model.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

/**
 * @Author：闫晓琪
 * @Filename：InterrogationController
 * @Package：com.smartmedical.doctor.interrogation.controller
 * @Project：smart-medical
 * @name：InterrogationController
 * @Date：2024/1/10 19:46
 */

@RestController
@Slf4j

public class InterrogationController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private InterrogationService interrogationService;

        @GetMapping("inquiry/verify/v1/findInquiryRecordList")
        public HealthR<List<DoctorHistoryInquiryRecordVo>> list(@RequestParam(name = "page", defaultValue ="1" ) Integer page, @RequestParam(name = "count", defaultValue = "2") Integer count) {
            startPage(page,count);
            List<DoctorHistoryInquiryRecordVo> doctorHistoryInquiryRecordVoList = interrogationService.list();
            return HealthR.ok(doctorHistoryInquiryRecordVoList,"查询成功");
        }




    /**
     * 查询用户详细信息
     * @param userId
     * @return
     */
    @GetMapping("inquiry/verify/v1/findUserInfo")
    public  HealthR<EchoUserVo> findUserInfo(@RequestParam(name = "userId") Integer userId) {
        EchoUserVo echoUserVoHealthR = interrogationService.findUserInfo(userId);
        return HealthR.ok(echoUserVoHealthR);
    }

    /**
     * 结束问诊
     * @param recordId
     * @return
     */
    @PutMapping("inquiry/verify/v1/endInquiry")
    public  Integer endInquiry(@RequestParam(name ="recordId") Integer recordId) {
        Integer endInquiry = interrogationService.endInquiry(recordId);
        return endInquiry;
    }

    /**
     * 查询问诊聊天记录
     * @return
     */
    @GetMapping("inquiry/verify/v1/findInquiryDetailsList")
    public HealthR<List<InquiryChatRecordVo>> findInquiryDetailsList(@RequestParam(name = "recordId") Integer recordId, @RequestParam(name = "page",defaultValue="1") Integer page, @RequestParam(name = "count", defaultValue = "20") Integer count){
       startPage(page,count);
        List<InquiryChatRecordVo> doctorHistoryInquiryRecordVoList = interrogationService.findInquiryDetailsList(recordId);
        return HealthR.ok(doctorHistoryInquiryRecordVoList);
    }
    /**
     * 医生-发送消息（文本消息）
     * @param inquiryId
     * @param content
     * @param type
     * @param userId
     */
    @PostMapping("inquiry/verify/v1/pushMessage")
    public HealthR pushMessage1(@RequestParam("inquiryId") Integer inquiryId,@RequestParam("content") String content,@RequestParam("type") Integer type,@RequestParam("userId") Integer userId) {
        return interrogationService.pushMessage1(inquiryId,content,type,userId);
    }


    /**
     * 查询医生历史问诊记录列表
     * @param page
     * @param count
     * @return
     */
    @GetMapping("inquiry/verify/v1/findHistoryInquiryRecord")
    public HealthR<List<DoctorHistoryInquiryRecordVo>> findHistoryInquiryRecord(
            @RequestParam("page") int page,
            @RequestParam("count") int count
    ) {
        startPage(page,count);
        List<DoctorHistoryInquiryRecordVo> records = interrogationService.findHistoryInquiryRecord();

        if (records != null && !records.isEmpty()) {
            return HealthR.ok(records);
        }
    return HealthR.fail();

    }

    /**
     * 查询问诊评价详情
     * @param recordId
     * @return
     */
    @GetMapping("verify/v1/findDoctorEvaluate")
    public HealthR<List<InquiryEvaluateMessage>> findDoctorEvaluate(@RequestParam Integer recordId) {
        List<InquiryEvaluateMessage> inquiryEvaluateMessages = interrogationService.findDoctorEvaluate(recordId);
        return HealthR.ok(inquiryEvaluateMessages);
    }



    private LoginUser getLogiUser() {
        String header = request.getHeader(TokenConstants.SESSION_ID);
        LoginUser loginUser = tokenService.getLoginUser(header);
        return loginUser;
    }









}
